﻿using LtccApi.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LtccApi.Controllers
{
    /// <summary>
    /// 汽车轮胎列表
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CarTireListController : Controller
    {
        private LtccContext mLtccContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ltccContex"></param>
        public CarTireListController(LtccContext ltccContex)
        {
            mLtccContext = ltccContex;
        }

        /// <summary>
        /// 根据轮胎型号获取对应的列表数据
        /// </summary>
        /// <param name="TireQuery"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult List(TireQuery tireQuery)
        {
            var qurerResult = mLtccContext.CarTireLists.AsQueryable();
            //过滤价格低于0
            qurerResult = qurerResult.Where(m => m.Price > 0);
            //过滤轮胎型号和查询关键字
            qurerResult = qurerResult.Where(m => m.FullName.Contains(tireQuery.TireSize) && m.FullName.Contains(tireQuery.SearchText));
            //过滤轮胎品牌
            if(tireQuery.TireBrand != "全部")
            {
                qurerResult = qurerResult.Where(m => m.FullName.Contains(tireQuery.TireBrand));
            }
            //过滤防爆
            if(tireQuery.AntiExplosion != "")
            {
                if (tireQuery.AntiExplosion == "防爆")
                {
                    qurerResult = qurerResult.Where(m => !m.FullName.Contains("不防爆") &&
                        m.FullName.Contains("防爆"));
                }
                else  //不防爆
                {
                    qurerResult = qurerResult.Where(m => m.FullName.Contains("不防爆"));
                }
            }
            //排序
            if(tireQuery.Sort == "高低")
            {
                qurerResult = qurerResult.OrderByDescending(m => m.Price);
            }
            else //低高
            {
                qurerResult = qurerResult.OrderBy(m => m.Price);
            }
            List<CarTireList> listData = qurerResult.Skip(tireQuery.IndexPage * tireQuery.PageSize).Take(tireQuery.PageSize).ToList();

            return Ok(listData);
        }
    }
}
